package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.vo.CatVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 商品类目实现类
 * @author sym
 * @version V1.0
 * @Project java2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 19:02
 */
@Service
public class CategoryServiceImpl  implements CategoryService {

    @Autowired
    private DtsCategoryMapper categoryMapper;

    @Value("{$aliyun.oss.endpoint}")
    private String endpoint;
    @Value("{$aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("{$aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("{$aliyun.oss.bucketName}")
    private String bucketName;


    @Override
    public IPage<DtsCategory> queryCategory(Integer id,String name,Integer page, Integer limit, String sort, String order) {

        //1:创建分页对象
        //参数1：当前页，参数2：每页显示条数
        IPage<DtsCategory> iPage = new Page<>(page,limit);

        //2:封装查询对象
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件不为空拼接查询条件
        if (id != null){
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //判断排序方式是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }
        }

        //3：查询并返回结果
        IPage<DtsCategory> resultPage =categoryMapper.selectPage(iPage, queryWrapper);

        return  resultPage;

    }

    /**
     * 添加
     * @param category
     * @return
     */
    @Override
    public DtsCategory add(DtsCategory category) {
        categoryMapper.insert(category);
        return category;
    }


    /**
     * 修改
     * @param category
     * @return
     */
    @Override
    public int update(DtsCategory category) {

     //设置修改时间
        category.setUpdateTime(new Date());
        int count = categoryMapper.updateById(category);
        return count;
    }

    /**
     * 删除
     * @param category
     */
    @Override
    public int delete(DtsCategory category) {
        int count = categoryMapper.deleteById(category);
        return count;
    }

    /**
     * 下拉
     * @return
     */
    @Override
    public List<CatVo> findCataVoCat() {
        List<CatVo>  findCataVoCat = categoryMapper.findCataVoCat();
        for (CatVo c :findCataVoCat) {
            Integer id = c.getValue();
            List<CatVo> children = categoryMapper.children(id);
            c.setChildren(children);
        }
        return findCataVoCat;
    }
}
